<?php
/**
 * Created by PhpStorm.
 * User: fyl
 * Date: 2016/11/21
 * Time: 14:14
 */

/**
 * Class Involved_UserModel
 */

class Involved_UserAwardModel extends BaseModel{

    protected $table = 'user_award';

    /**
     * @param $user_id
     * @return mixed
     * 获取用户红包个数
     */
    public function AwardCount($user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->count();
    }

    /**
     * @param $user_id
     * @return mixed
     * 获取用户红包数据
     */
    public function GetUserAward($user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->get();
    }

    /**
     * @param $AwardId
     * @return mixed
     * 获取项目红包数据
     */
    public function ItemAward($AwardId)
    {
        return DB::table($this->table)
            ->whereIn('auto_id',$AwardId)
            ->get();
    }

    /**
     * @param $user_id
     * @return mixed
     * 获取项目红包数据
     */
    public function awardAmount($user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('status','ACTIVATE')
            //->orWhere('status','HAS_USED')
            ->sum('amount');
    }
    /**
     * 查询用户的红包
     */
    public function SelectUserAward($awardids){
       return DB::table($this->table)
            ->whereIn('auto_id', $awardids)
            ->where('status','HAS_USED')
            ->get();

    }
    public function updateAwardStatus($awardids){
        return DB::table($this->table)
            ->whereIn('auto_id', $awardids)
            ->update(array('status' => 'ACTIVATE','active_time'=>time()));

    }

    /**
     * @param $user_id
     * @return mixed
     * 获取可用红包数
     */
    public function AwardNum($user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('status','AVAILABLE')
            ->count();
    }

    /**
     * @param $user_id
     * @return mixed
     * 根据红包状态获取数据
     */
    public function UserAward($statue,$user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('status',$statue)
            //->get();
            ->paginate(8);
    }
    /**
     * 是否存在可用的红包
     */
    public function IsAward($user_id){
        return DB::table($this->table)
            ->select('auto_id','amount','restrict_amount','status','active_time','due_time')
            ->where('user_id',$user_id)
            ->where('due_time', '>', time())
            ->get();
    }
    /**.0.1
     * 检索最优红包
     */
    public function SelectAward($paramObj){
        return  DB::table($this->table)
            ->select('auto_id','amount','restrict_amount','status','active_time','due_time','limit')
            ->where('user_id',$paramObj->user_id)
            ->where('status', '=', 'AVAILABLE')
            ->where('due_time', '>', time())
            ->orderBy('auto_id', 'desc')
            ->get();



    }
    //30天
    public function Thirty($paramObj){
        return  DB::table($this->table)
            ->select('auto_id','amount','restrict_amount','status','active_time','due_time','limit')
            ->where('user_id',$paramObj->user_id)
            ->where('status', '=', 'AVAILABLE')
            ->where('due_time', '>', time())
            ->where('limit', '=',30)
            ->orderBy('auto_id', 'desc')
            ->get();
    }
    //60天
    public function Sixty($paramObj){
        return  DB::table($this->table)
            ->select('auto_id','amount','restrict_amount','status','active_time','due_time','limit')
            ->where('user_id',$paramObj->user_id)
            ->where('status', '=', 'AVAILABLE')
            ->where('due_time', '>', time())
            ->where(function($query){
                $query->where('limit','=' ,30)
                    ->orWhere(function($query){
                        $query->where('limit','=' ,60);
                    });
            })
            ->orderBy('auto_id', 'desc')
            ->get();
    }
    //大于60天
    public function Much($paramObj){
        return  DB::table($this->table)
            ->select('auto_id','amount','restrict_amount','status','active_time','due_time','limit')
            ->where('user_id',$paramObj->user_id)
            ->where('status', '=', 'AVAILABLE')
            ->where('due_time', '>', time())
            ->where('limit', '=',60)
            ->orderBy('auto_id', 'desc')
            ->get();
    }

    /**
     * @return mixed
     * 获取项目红包数据
     */
    public function AwardAmountAll()
    {
        return DB::table($this->table)
            ->where('status','ACTIVATE')
            ->sum('amount');
    }

    /***                                             API红包                                                                                                                               ***/
    /**
     * @param $user_id
     * @return mixed
     * 红包
     */
    public function APIUserAward($user_id,$page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return DB::table($this->table)
            ->select('auto_id','amount','restrict_amount','status','due_time','limit','user_id')
            ->where('user_id',$user_id)
            ->orderBy('auto_id', 'desc')
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->get();
    }

    /***                                             web红包                                                                                                                               ***/
    /**
     * @param $user_id
     * @return mixed
     * 红包
     */
    public function WebUserAward($user_id,$page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return DB::table($this->table)
            ->select('auto_id','amount','restrict_amount','status','due_time','limit','user_id')
            ->where('user_id',$user_id)
            ->orderBy('auto_id', 'desc')
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->get();
    }
    /**
     * 检索单挑红包
     */
    public function FindAward($auto_id){
        return DB::table($this->table)
            ->where('auto_id',$auto_id)
            ->first();
    }

    /**
     * 红包未使用过期
     * @param $user_id
     * @return mixed
     */
    public function MyUserRed($user_id)
    {
        return DB::table($this->table)
            ->select('auto_id','due_time','status')
            ->where('user_id',$user_id)
            ->where('due_time','<',time())
            ->where('status','AVAILABLE')
            ->get();
    }

}